package com.ivy.conn;



import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;



/**
 * Created by Administrator on 2018/1/12.
 */

public class Login {
    private static String cookie;
    public static String getCookie() {
		return cookie;
	}
    private static ByteArrayOutputStream stream;

    public static ByteArrayOutputStream getStream() {
        return stream;
    }

    @Deprecated
    public static String login(String path , String referer){

        String cookie = null;

        try {

            URL url=new URL(path);
            HttpURLConnection con=(HttpURLConnection)url.openConnection();
            con.setRequestMethod("GET");
            con.setDoOutput(true);
            con.setDoInput(true);
            con.setRequestProperty("Referer", referer);
            InputStream in=con.getInputStream();
            cookie = con.getHeaderField("Set-Cookie");
            System.out.println(cookie);

            stream = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while((len=in.read(buffer))!=-1){
                stream.write(buffer, 0, len);
            }

            in.close();
            con.disconnect();

            return cookie;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    public static byte[] getValidate() {
    	String cookiepath = "http://1.85.55.152/sys/ValidateCode.aspx?t=807";
		String referer="http://1.85.55.152/ZNPK/TeacherKBFB.aspx";
		int max=10240;
		byte[] buf=new byte[max];
		int end=0;
		
		try {
			URL url=new URL(cookiepath);
			HttpURLConnection con=(HttpURLConnection)url.openConnection();
			con.setRequestMethod("GET");
			con.setDoOutput(true);
			con.setDoInput(true);
			con.setRequestProperty("Referer", referer);
			InputStream in=con.getInputStream();
			
			String all = con.getHeaderField("Set-Cookie");
            String [] ctemp = all.split(";");
            cookie = ctemp[0];
            System.out.println(cookie + "=====this is Login method=======");
            
//			int len=0;
//			len=in.read(buf);
//			end = cutJpg(buf, len);

            
            stream = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            while((len=in.read(buffer))!=-1){
                stream.write(buffer, 0, len);
            }
            
			String uuid = "yzm";
			String location = "E:\\workspace\\eclipse\\jpg\\"+uuid+".jpeg";
			OutputStream out = new FileOutputStream(location);
			out.write(stream.toByteArray(), 0, stream.toByteArray().length);
			out.close();
			
			in.close();
			stream.close();
			con.disconnect();
			
		}  catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		return Arrays.copyOfRange(buf, 0, end);
		return stream.toByteArray();

	}
	private static  int cutJpg(byte[] buf,int len) {
		int end=0;
		for(int i=0;i<len-1;i++) {
			byte p=buf[i];
			byte n=buf[i+1];
			if(p==-1 && n==-39) {
				end=i+2;
				break;
			}
		}
		return end;
		
	}

	public static void main(String[] args){
		Login login = new Login();
		String cookiepath = "http://1.85.55.152/sys/ValidateCode.aspx?t=807";
		String referer="http://1.85.55.152/ZNPK/TeacherKBFB.aspx";
		byte[] imgBuf = login.getValidate();
		for(int i = 0 ; i < imgBuf.length ;i++){
			System.out.println(imgBuf[i]);
		}
	}
	
}
